package leetcode.code0891;

import java.util.Arrays;

public class Solution {

	int mod = (int) 1e9 + 7;

	public int sumSubseqWidths(int[] nums) {
		Arrays.sort(nums);
		long ans = 0;
		for (int i = 0; i < nums.length; i++) {
			int num = nums[i];
			ans += (powMod(2, i) - powMod(2, nums.length - i - 1)) * num;
			ans %= mod;
		}
		return (int) ans;
	}

	private long powMod(long a, int pow) {
		long r = 1;
		while (pow > 0) {
			if ((pow & 1) == 1) {
				r = (r * a) % mod;
			}
			a = a * a % mod;
			pow >>= 1;
		}
		return r;
	}
}
